//
//  ModalViewCell.swift
//  Automobile
//
//  Created by erkebulan elzhan on 8/2/20.
//  Copyright © 2020 erkebulan elzhan. All rights reserved.
//

import UIKit
import SnapKit

class ModalViewCell: UITableViewCell {
    private lazy var titleLbl: UILabel = {
        let lbl = UILabel()
        lbl.font = UIFont.systemFont(ofSize: 15, weight: .bold)
        lbl.textColor = .mediumDarkBlue
        lbl.textAlignment = .left
        lbl.sizeToFit()
        return lbl
    }()
    private lazy var colorView: UIView = {
        let view = UIView()
        view.clipsToBounds = true
        view.layer.cornerRadius = 10
        return view
    }()
    private lazy var checkBoxWrapper: UIView = {
        let view = UIView ()
        view.layer.borderColor = UIColor.greyBlue.cgColor
        view.layer.borderWidth = 1
        view.layer.cornerRadius = 5
        return view
    }()
    
    private lazy var checkBox: UIImageView = {
        let btn = UIImageView()
        btn.contentMode = .scaleAspectFit
        return btn
    }()
    
    var data: ModalView.ModalData! {
        didSet {
            self.titleLbl.text = data.name
            self.updateCheckbox()
            self.updateColor()
        }
    }
    
    
    private func updateCheckbox () {
        if (data.isSelected ?? false) {
            self.checkBox.image = UIImage(named: "correct")
            self.checkBoxWrapper.backgroundColor = .blueColor
            self.checkBoxWrapper.layer.borderWidth = 0.0
        } else {
            self.checkBox.image = nil
            self.checkBoxWrapper.backgroundColor = .clear
            self.checkBoxWrapper.layer.borderWidth = 1
        }
    }
    private func updateColor () {
        if data.color != nil {
            colorView.backgroundColor = data.color!
            colorView.snp.updateConstraints { (make) in
                make.width.equalTo(20)
                make.trailing.equalTo(titleLbl.snp.leading).offset(-8)
            }
        } else {
            colorView.snp.updateConstraints { (make) in
                make.width.equalTo(0)
                make.trailing.equalTo(titleLbl.snp.leading)
            }
        }
    }
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.addViews()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    private func addViews () {
        self.contentView.addSubview(titleLbl)
        self.contentView.addSubview(colorView)
        self.contentView.addSubview(checkBoxWrapper)
        self.checkBoxWrapper.addSubview(checkBox)
        self.addConstraints()
    }
    
    private func addConstraints () {
        
        colorView.snp.makeConstraints { (make) in
            make.width.equalTo(0)
            make.height.equalTo(20)
            make.leading.centerY.equalToSuperview()
            make.trailing.equalTo(titleLbl.snp.leading)
        }
        checkBoxWrapper.snp.makeConstraints { (make) in
            make.width.height.equalTo(16)
            make.trailing.equalToSuperview().offset(-16)
            make.centerY.equalToSuperview()
        }
        checkBox.snp.makeConstraints { (make) in
            make.width.equalTo(9)
            make.height.equalTo(6)
            make.center.equalToSuperview()
        }
        titleLbl.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview()
        }
    }
    
}
